<?php
namespace app\index\controller;
use app\model\User as UserModel;
use think\Db;


class User extends Base
{
    public $exception = ['login','getLoginSms'];
    
    public function __construct(){
        $model = new UserModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '分类';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function index($id){
        $data = UserModel::where('id',$id)->find();
        if (!empty($data['redirect_cid'])) {
            return redirect(url('index',['id'=>$data['redirect_cid']]).'#'.$id);
        }
        // 特殊页面，特殊处理
        if (in_array($data['view_url'], ['activity'])) {
            // $activitys = Db::name('activity')->field('content',true)->limit(4)->select();
            // $this->assign([
            //     'activitys'=>$activitys
            // ]);
            return redirect('index/activity');
        }
        if (!empty($data['view_url'])) {
            $view = $data['view_url'];
        }elseif ($data['type'] == 1) {
            $view = 'detail';
        }else{
            $view = 'index';
        }
        return view($view, [
            'data'=>$data
        ]);
    }

    public function login(){
        if (request()->isPost()) {
            $model = $this->model;
            $fields = [
                'must'=>['mobile','verify'],
                'nomust'=>['is_cookie']
            ];
            $param = paramFilter(request()->param(),$fields);
            if (!empty($param['error_msg'])) {
                $this->error($param['error_msg']);
            }

            $mobile = $param['mobile'];
            if (!isMobile($mobile)) {
                $this->error('手机号格式错误');    
            }
            if (empty($param['verify']) || $param['verify'] != session('sms_'.$mobile)) {
                $this->error('验证码错误');       
            }

            $map = [
                ['mobile','=',$mobile]
            ];
            $data = $model->where($map)->find();
            if ( empty($data) ) {
                $save_data = [
                    'mobile'=>$mobile,
                    'ctime'=>time(),
                    'nickname'=>'用户'.date('His')
                ];
                // $save_data['headimgurl'] = Db::name('config')->where('key','USER_DEFAULT_IMG')->value('value');
                
                if ( Db::name('user')->insert($save_data) ) {
                    $data = Db::name('user')->where('mobile',$save_data['mobile'])->find();
                }else{
                    $this->error("注册失败");
                }
            }
            if ($data['status'] == 0) {
                $this->error("请等待业务人员开通授权");
            }

            session('sms_'.$mobile,null);
            session('cch_user',$data);
            if ($param['is_cookie'] == 'on') {
                cookie('cch_user_id',$data['id'],3600*24*7);
            }
            
            // $login_redirect_url = session('login_redirect_url');
            // if (request()->host() == 'video.deyunai.com') {
            //     $login_redirect_url = url('index/baidu_video/index');
            // }elseif (empty($login_redirect_url)) {
            //     $login_redirect_url = url('index/index');
            // }
            $login_redirect_url = url('index/index');
            return redirect($login_redirect_url);
        }
        return view('login', [
            
        ]);
    }

    public function getLoginSms($mobile='',$is_check=0){
        if (strlen($mobile) != 11) {
            $this->error('手机号格式错误');    
        }
        $model = $this->model;
        if ($is_check == 1 && $model->where('mobile',$mobile)->count() > 0) {
            $this->error('手机号已被占用'); 
        }
        $code = rand(1000,9999);
        $sms_res = sendSms($mobile,'您的验证码是：'.$code,'得力云');
        // $code = 1234;
        // $sms_res['status'] = 1;
        if ($sms_res['status'] == 1) {
            session('sms_'.$mobile,$code);
            $this->success('发送成功');    
        }else{
            $this->error($sms_res['msg']);  
        }
    }

}
